
import 'dart:math';

import 'package:flutter/material.dart';

class ListStateBgPainter extends CustomPainter{

  int state;
  double frameWidth;

  int offset = 4;

  ListStateBgPainter(this.frameWidth,this.state);

  @override
  void paint(Canvas canvas, Size size) {
    drawBg(canvas,size);
    drawStateColor(canvas,size);
    drawBorder(canvas,size);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }

  void drawBg(Canvas canvas, Size size) {
    Path path = Path();
    path.moveTo(0, frameWidth);
    path.lineTo(frameWidth, 0);
    path.lineTo(size.width, 0);
    path.lineTo(size.width-frameWidth, frameWidth);
    path.lineTo(0, frameWidth);

    path.lineTo(0, size.height);
    path.lineTo(size.width-frameWidth, size.height);
    path.lineTo(size.width-frameWidth, frameWidth);
    path.close();
    Paint bgPaint = Paint();
    bgPaint.color = Colors.white;
    bgPaint.strokeWidth = 0.5;
    bgPaint.isAntiAlias = true;
    bgPaint.style = PaintingStyle.fill;
    canvas.drawPath(path, bgPaint);

    path.reset();
    path.moveTo(size.width, 0);
    path.lineTo(size.width, size.height-frameWidth);
    path.lineTo(size.width-frameWidth, size.height);
    path.lineTo(size.width-frameWidth, frameWidth);
    path.close();
    canvas.drawPath(path, bgPaint);

  }

  void drawStateColor(Canvas canvas, Size size) {

    Paint bgPaint = Paint();
    bgPaint.strokeWidth = 0.5;
    bgPaint.isAntiAlias = true;
    bgPaint.style = PaintingStyle.fill;

    if(state==0 || state == 1){ ///普通梯没开门
      Path path = Path();
      path.moveTo(0, frameWidth);
      path.lineTo(frameWidth, frameWidth);
      path.lineTo(frameWidth, size.height);
      path.lineTo(0, size.height);
      path.close();
      bgPaint.color = Colors.transparent;
      canvas.drawPath(path, bgPaint);

      path.reset();
      path.moveTo(size.width - 2*frameWidth, frameWidth);
      path.lineTo(size.width - frameWidth, frameWidth);
      path.lineTo(size.width - frameWidth, size.height);
      path.lineTo(size.width - 2*frameWidth, size.height);
      path.close();
      canvas.drawPath(path, bgPaint);

    }else if(state == 2){ ///正门开
      Path path = Path();
      path.moveTo(0, frameWidth);
      path.lineTo(frameWidth+offset, frameWidth);
      path.lineTo(frameWidth+offset, size.height);
      path.lineTo(0, size.height);
      path.close();
      bgPaint.color = Colors.yellow;
      canvas.drawPath(path, bgPaint);

      path.reset();
      path.moveTo(size.width - 2*frameWidth-offset, frameWidth);
      path.lineTo(size.width - frameWidth, frameWidth);
      path.lineTo(size.width - frameWidth, size.height);
      path.lineTo(size.width - 2*frameWidth-offset, size.height);
      path.close();
      canvas.drawPath(path, bgPaint);

    }else if(state == 3){ ///侧门开
      Path path = Path();
      Point p = getPointOnLine(size.width - frameWidth,frameWidth,size.width,0,0.4);
      path.moveTo(size.width - frameWidth,frameWidth);
      path.lineTo(p.x.toDouble(), p.y.toDouble());
      p = getPointOnLine(size.width - frameWidth,size.height,size.width,size.height-frameWidth,0.4);
      path.lineTo(p.x.toDouble(), p.y.toDouble());
      path.lineTo(size.width - frameWidth,size.height);
      path.close();
      bgPaint.color = Colors.orange;
      canvas.drawPath(path, bgPaint);

      path.reset();
      p = getPointOnLine(size.width - frameWidth,frameWidth,size.width,0,0.6);
      path.moveTo(p.x.toDouble(), p.y.toDouble());
      path.lineTo(size.width,0);
      p = getPointOnLine(size.width - frameWidth,size.height,size.width,size.height-frameWidth,0.6);
      path.lineTo(size.width,size.height-frameWidth);
      path.lineTo(p.x.toDouble(),p.y.toDouble());
      path.close();
      canvas.drawPath(path, bgPaint);
    }


  }

  void drawBorder(Canvas canvas, Size size) {
    Path path = Path();
    path.moveTo(0, frameWidth);
    path.lineTo(frameWidth, 0);
    path.lineTo(size.width, 0);
    path.lineTo(size.width-frameWidth, frameWidth);
    path.lineTo(0, frameWidth);

    path.lineTo(0, size.height);
    path.lineTo(size.width-frameWidth, size.height);
    path.lineTo(size.width-frameWidth, frameWidth);

    path.moveTo(size.width, 0);
    path.lineTo(size.width, size.height-frameWidth);

    path.moveTo(size.width, size.height-frameWidth);
    path.lineTo(size.width-frameWidth, size.height);
    path.close();

    Paint bgPaint = Paint();
    bgPaint.color = Colors.black;
    bgPaint.strokeWidth = 0.5;
    bgPaint.isAntiAlias = true;
    bgPaint.style = PaintingStyle.stroke;
    canvas.drawPath(path, bgPaint);
  }

  Point getPointOnLine(double x1, double y1, double x2, double y2, double t) {
    double x = x1 + (x2 - x1) * t;
    double y = y1 + (y2 - y1) * t;
    return Point(x, y);
  }

}